//
//  File.swift
//  abomb
//
//  Created by changwenyuan on 14-11-27.
//  Copyright (c) 2014年 cleanland. All rights reserved.
//  字符串扩展

import Foundation

extension Character {
    func utf8Value() -> UInt8 {
        for s in String(self).utf8 {
            return s
        }
        return 0
    }
    
    func utf16Value() -> UInt16 {
        for s in String(self).utf16 {
            return s
        }
        return 0
    }
    
    func unicodeValue() -> UInt32 {
        for s in String(self).unicodeScalars {
            return s.value
        }
        return 0
    }
}

public extension String{
    subscript (r: Range<Int>) -> String {
        get {
            let startIndex = advance(self.startIndex, r.startIndex)
            let endIndex = advance(startIndex, r.endIndex - r.startIndex)
            
            return self[Range(start: startIndex, end: endIndex)]
        }
    }
    
    //分割字符
    public func split(s:String) -> [String] {
        if s.isEmpty{
            var x=[String]()
            for y in self{
                x.append(String(y))
            }
            return x
        }
        return self.componentsSeparatedByString(s)
    }
    
    //去掉左右空格
    public func trim()->String{
        return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
    }
    
    
    public func trim(eachCharOfStr:String)->String{
        return self.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: eachCharOfStr));
    }
    
    //是否包含字符串
    public func has(s:String)->Bool{
        if (self.rangeOfString(s) != nil) {
            return true
        }else{
            return false
        }
    }
    //是否包含前缀
    public func hasBegin(s:String)->Bool{
        if self.hasPrefix(s) {
            return true
        }else{
            return false
        }
    }
    //是否包含后缀
    public func hasEnd(s:String)->Bool{
        if self.hasSuffix(s) {
            return true
        }else{
            return false
        }
    }
    //统计长度
    public func length()->Int{
        return count(self)
    }
    
    //统计长度(别名)
    public func size()->Int{
        return count(self)
    }
    
    //截取字符串
    public func substr(from:Int,to:Int)->String{
        if self == ""
        {
            return "";
        }
        var toto  = min(to,self.length());
        return
            self
                .substringFromIndex(advance(self.startIndex,from))
                .substringToIndex(advance(self.startIndex,toto));
    }
    
    //替换
    public func replace(oldstr:String,to:String)->String{
        return
            self.stringByReplacingOccurrencesOfString(
                oldstr,
                withString: to,
                options: NSStringCompareOptions.LiteralSearch, range: nil);
    }
    
    //截取字符串
    public func substr(from:Int)->String{
        return substringFromIndex(advance(self.startIndex,from));
    }

    //重复字符串
    public func repeat(times: Int) -> String{
        var result = ""
        for i in 0...times {
            result += self
        }
        return result
    }
    
    //反转
    public func reverse()-> String{
        var s=self.split("").reverse()
        var x=""
        for y in s{
            x+=y
        }
        return x
    }
}



//use: var str = "hangge.com"  print(str[7,3]) print(str[7])
//给字符串String类添加下标脚本，支持索引访问
public extension String
{
    subscript(start:Int, length:Int) -> String
        {
        get{
            return (self as NSString).substringWithRange(NSRange(location: start, length: length))
        }
        set{
            let tmp = self
            var s = ""
            var e = ""
            for (idx, item) in enumerate(tmp) {
                if(idx < start)
                {
                    s += "\(item)"
                }
                if(idx >= start + length)
                {
                    e += "\(item)"
                }
            }
            self = s + newValue + e
        }
    }
    
    subscript(index:Int) -> String
        {
        get{
            return String(self[self.startIndex])
        }
        set{
            let tmp = self
            self = ""
            for (idx, item) in enumerate(tmp) {
                if idx == index {
                    self += "\(newValue)"
                }else{
                    self += "\(item)"
                }
            }
        }
    }
}